from GraphSettings.GraphCmds.ContextMenuCmds import ContextMenuCmds;
from PyQt4 import QtCore, QtGui;

class ViewCanvas(QtGui.QGraphicsView):
    def __init__(self, parent=None, graphScene=None):
        QtGui.QGraphicsView.__init__(self, parent.mainWin);
        self.setDragMode(QtGui.QGraphicsView.RubberBandDrag);
        self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse);
        self.setResizeAnchor(QtGui.QGraphicsView.AnchorViewCenter);
        self.bKeyShift, self.bPanning, self.bDragging = False, False, False;
        self.graphicsScene = graphScene;
        self.parent = parent;
        self.mWin = parent.mainWin;
        self.nZoom = 1;
        self.sMsg = "Zoom: ";
        
    def createContextMenu(self, actionCmds):
        self.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu);        
        self.objContextMenuCmds = ContextMenuCmds(self, self.graphicsScene, actionCmds);
        self.contextMenu = self.objContextMenuCmds.contextMenu;
        
    def contextMenuEvent(self, event):
        super(ViewCanvas, self).contextMenuEvent(event);
        self.contextMenu.popup(self.mapToGlobal(event.pos()));
        
    def zoomIn(self):
        self.scale(1.1, 1.1);
        self.nZoom *= 1.1;
        sMsg = self.sMsg + str(round(self.nZoom*100,2)) + "%";
        self.parent.statusbar.showMessage(sMsg);
        
    def zoomOut(self):
        self.scale(0.9, 0.9);
        self.nZoom *= 0.9;
        sMsg = self.sMsg + str(round(self.nZoom*100,2)) + "%";
        self.parent.statusbar.showMessage(sMsg);
        
    def zoomReset(self):
        self.resetTransform();
        self.nZoom = 1;
        sMsg = self.sMsg + str(round(self.nZoom*100,2)) + "%";
        self.parent.statusbar.showMessage(sMsg);
        
    def setZoom(self, nZoom):
        self.resetTransform();
        self.scale(nZoom[0], nZoom[0]);
        self.nZoom = nZoom[0];
        sMsg = self.sMsg + str(round(self.nZoom*100,2)) + "%";
        self.parent.statusbar.showMessage(sMsg);
        
    def getZoom(self):
        return [self.nZoom];
        
    def wheelEvent(self, event):
        nDelta = 1.3 if (event.delta() > 0) else 0.7;
        self.nZoom *= nDelta;
        self.scale(nDelta, nDelta);
        sMsg = self.sMsg + str(round(self.nZoom*100,2)) + "%";
        self.parent.statusbar.showMessage(sMsg);
        
    def keyPressEvent(self, event):
        super(ViewCanvas, self).keyPressEvent(event);
        #Turn On Panning Mode (& Turn Off RubberBand Selection)
        if (event.key() == QtCore.Qt.Key_P):
            self.setDragMode(QtGui.QGraphicsView.ScrollHandDrag);
    
    def keyReleaseEvent(self, event):
        super(ViewCanvas, self).keyReleaseEvent(event);
        #Turn On RubberBand Selection (& Turn Off Panning Mode)
        if (event.key() == QtCore.Qt.Key_P):
            self.setDragMode(QtGui.QGraphicsView.RubberBandDrag);
    
    def mousePressEvent(self, event):
        super(ViewCanvas, self).mousePressEvent(event);
            
    def mouseMoveEvent(self, event):
        super(ViewCanvas, self).mouseMoveEvent(event);
        
    def mouseReleaseEvent(self, event):
        super(ViewCanvas, self).mouseReleaseEvent(event);

    def createScene(self):
        self.graphicsScene.setCanvasView(self);
#         self.graphicsScene.buildDataGraph();
        self.setScene(self.graphicsScene);
        self.show();
